package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.vo.FriendListVo;
import com.xk857.simpleeffect.domain.vo.FriendVo;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import cn.hutool.core.collection.CollUtil;

public class ListSelectFriendAdapter extends BaseQuickAdapter<FriendListVo, ListSelectFriendAdapter.ViewHolder> {

    private final Context mContext;
    // 用户选择时触发
    private final Consumer<List<Long>> selectConsumer;
    // 已选择的好友id
    private List<Long> oldSelectList;
    private List<Long> selectList;

    /**
     * @param context 环境上下文
     * @param voList 好友列表信息
     * @param selectConsumer 监听用户的选择事件
     */
    public ListSelectFriendAdapter(Context context, List<FriendListVo> voList, Consumer<List<Long>> selectConsumer) {
        super(voList);
        this.selectList = new ArrayList<>();
        this.oldSelectList = new ArrayList<>();
        this.mContext = context;
        this.selectConsumer = selectConsumer;
    }

    /**
     * @param context 环境上下文
     * @param voList 好友列表信息
     * @param selectConsumer 监听用户的选择事件
     * @param oldSelectList 已经选择的用户id
     */
    public ListSelectFriendAdapter(Context context, List<FriendListVo> voList, List<Long> oldSelectList, Consumer<List<Long>> selectConsumer) {
        super(voList);
        this.oldSelectList = oldSelectList;
        this.mContext = context;
        this.selectConsumer = selectConsumer;
        this.selectList = new ArrayList<>();
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int i, @Nullable FriendListVo friendList) {
        holder.pinyinTextView.setText(friendList.getKey());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        holder.recyclerView.setLayoutManager(layoutManager);
        ListFriendItemAdapter adapter = new ListFriendItemAdapter(mContext, friendList.getFriendList(), oldSelectList,selectList, selectConsumer);
        holder.recyclerView.setAdapter(adapter);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup parent, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_friend, parent, false));
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView pinyinTextView;
        RecyclerView recyclerView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.pinyinTextView = itemView.findViewById(R.id.pinyin_text_view);
            this.recyclerView = itemView.findViewById(R.id.recycler_view);
        }
    }

    /**
     * 子布局
     */
    static class ListFriendItemAdapter extends BaseQuickAdapter<FriendVo, ListFriendItemAdapter.ViewHolder> {

        private final Context mContext;
        private List<Long> oldSelectList;
        private List<Long> selectList;
        //
        private Consumer<List<Long>> selectConsumer;

        public ListFriendItemAdapter(Context context, List<FriendVo> voList, List<Long> oldSelectList,List<Long> selectList,  Consumer<List<Long>> selectConsumer) {
            super(voList);
            this.mContext = context;
            this.oldSelectList = oldSelectList;
            this.selectList = selectList;
            this.selectConsumer = selectConsumer;
        }


        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int i, @Nullable FriendVo friend) {
            Glide.with(mContext).load(friend.getHeadImg()).into(holder.headImgView);
            holder.nickNameText.setText(friend.getNickName());
            holder.mainLayout.setOnClickListener(v -> {
                if (holder.smoothCheckBox.isEnabled()) {
                    holder.smoothCheckBox.setChecked(!holder.smoothCheckBox.isChecked());
                }
            });
            // 如果选择过了，设置为选中状态
            if (CollUtil.isNotEmpty(oldSelectList)) {
                for (Long friendId : oldSelectList) {
                    if (friend.getToId().equals(friendId)) {
                        holder.smoothCheckBox.setChecked(true);
                        holder.smoothCheckBox.setEnabled(false);
                        break;
                    }
                }
            }

            holder.smoothCheckBox.setOnCheckedChangeListener((checkBox, isChecked) -> {
                if (isChecked && checkBox.isEnabled()) {
                    selectList.add(friend.getToId());
                } else {
                    selectList.remove(friend.getToId());
                }
                selectConsumer.accept(selectList);
            });
        }

        @NonNull
        @Override
        protected ViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup parent, int i) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_friend_item_select, parent, false));
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            ImageView headImgView;
            TextView nickNameText;
            LinearLayout mainLayout;
            CheckBox smoothCheckBox;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                this.headImgView = itemView.findViewById(R.id.head_img_view);
                this.nickNameText = itemView.findViewById(R.id.nick_name_text);
                this.mainLayout = itemView.findViewById(R.id.main_layout);
                this.smoothCheckBox = itemView.findViewById(R.id.smooth_check_box);
            }

        }
    }

}
